--[[=========================================================
	VIEW TRANSFORM INFO
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Constructor
--[[-----------------------------------------------------]]--
ObjViewTransformInfo			= class( function( self, id, pos, ang, fov, dist )

	--	Members
	self.TableName				= "ViewTransformInfo"
	self.ID						= ValOrDef( id, "integer", 0 )
	self.Position				= ValOrDef( pos, "Vector", Vector( 0, 0, 0 ) )
	self.Angles					= ValOrDef( ang, "Angle", Angle( 0, 0, 0 ) )
	self.FOV					= ValOrDef( fov, "number", 0 )
	self.CheckDistance			= ValOrDef( dist, "number", 0 )
	
--	SQL management - SERVER only
if SERVER then
	
	--	SQL Insert ( table, ids, contents )
	self.Insert					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Generate a new ID
		if self.ID == 0 then
			self.ID					=
				sql.GetNewID(
					self.TableName,
					FieldInfo( "ID", self.ID, "number" )
				)
		end
		
		--	Insert in DB if we got an ID
		if self.ID != 0 then
			result = sql.Insert(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "PositionX", self.Position.x, "number" ),
				FieldInfo( "PositionY", self.Position.y, "number" ),
				FieldInfo( "PositionZ", self.Position.z, "number" ),
				FieldInfo( "AngleP", self.Angles.p, "number" ),
				FieldInfo( "AngleY", self.Angles.y, "number" ),
				FieldInfo( "AngleR", self.Angles.r, "number" ),
				FieldInfo( "FOV", self.FOV, "number" ),
				FieldInfo( "CheckDistance", self.CheckDistance, "number" )
			)
		end
	end
	
	--	SQL Update ( table, ids, contents )
	self.Update					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Update in DB
		result = sql.Update(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" ),
			FieldInfo( "PositionX", self.Position.x, "number" ),
			FieldInfo( "PositionY", self.Position.y, "number" ),
			FieldInfo( "PositionZ", self.Position.z, "number" ),
			FieldInfo( "AngleP", self.Angles.p, "number" ),
			FieldInfo( "AngleY", self.Angles.y, "number" ),
			FieldInfo( "AngleR", self.Angles.r, "number" ),
			FieldInfo( "FOV", self.FOV, "number" ),
			FieldInfo( "CheckDistance", self.CheckDistance, "number" )
		)
	end
	
	--	SQL Delete ( table, ids, id contents )
	self.Delete					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Delete from DB
		result = sql.Delete(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" )
		)
		
		--	Reset ID if it was deleted
		if result then
			self.ID					= 0
		end
	end
	
	--	SQL Reload ( table, ids, id contents )
	self.Reload					= function( self, id )
	
		--	Redefinie id if needed
		if IsInt( id ) then
			self.ID				= id
		end
	
		--	Retrieve from DB
		self:FillProperties(
			sql.GetRow(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "PositionX", self.Position.x, "number" ),
				FieldInfo( "PositionY", self.Position.y, "number" ),
				FieldInfo( "PositionZ", self.Position.z, "number" ),
				FieldInfo( "AngleP", self.Angles.p, "number" ),
				FieldInfo( "AngleY", self.Angles.y, "number" ),
				FieldInfo( "AngleR", self.Angles.r, "number" ),
				FieldInfo( "FOV", self.FOV, "number" ),
				FieldInfo( "CheckDistance", self.CheckDistance, "number" )
			)
		)
	end
	
	--	Assign fields from row
	self.FillProperties			= function( self, row )
		self.Position			= Vector(
									tonumber( row["PositionX"] ),
									tonumber( row["PositionY"] ),
									tonumber( row["PositionZ"] ) )
		self.Angles				= Angle( 
									tonumber( row["AngleP"] ),
									tonumber( row["AngleY"] ),
									tonumber( row["AngleR"] ) )
		self.FOV				= tonumber( row["FOV"] )
		self.CheckDistance		= tonumber( row["CheckDistance"] )
	end
	
end	--	End of SQL management

--	Other server functions
if SERVER then

	--	Returns a table for transfers to clients
	self.GetClientTable				= function( self )
		return {
			["ID"]					= self.ID,
			["Position"]			= self.Position,
			["Angles"]				= self.Angles,
			["FOV"]					= self.FOV,
			["CheckDistance"]		= self.CheckDistance
		}
	end

end

end)